package com.macro.mall.tiny.modules.pms.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.macro.mall.tiny.common.api.CommonResult;
import com.macro.mall.tiny.common.api.ResultCode;
import com.macro.mall.tiny.modules.pms.model.PmsCoolTool;
import com.macro.mall.tiny.modules.pms.model.PmsElectricMasterData;
import com.macro.mall.tiny.modules.pms.model.PmsFactoryData;
import com.macro.mall.tiny.modules.pms.model.PmsRepairData;
import com.macro.mall.tiny.modules.pms.service.PmsCoolToolService;
import com.macro.mall.tiny.modules.pms.service.PmsElectricMasterDataService;
import com.macro.mall.tiny.modules.pms.service.PmsFactoryDataService;
import com.macro.mall.tiny.modules.pms.service.PmsRepairDataService;
import com.macro.mall.tiny.modules.ums.model.UmsMember;
import com.macro.mall.tiny.security.util.MemberContext;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.web.servlet.error.ErrorMvcAutoConfiguration;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * @author Tyrone
 * @date 2024/11/16 17:11
 */
@RestController
@RequestMapping("pms/vipAccess")
@Api(tags = "PmsVipAccessController")
@Tag(name = "PmsVipAccessController", description = "vip权限控制")
public class PmsVipAccessController {

    @Autowired
    PmsFactoryDataService pmsFactoryDataService;

    @Autowired
    private PmsCoolToolService pmsCoolToolService;

    @Autowired
    private PmsElectricMasterDataService pmsElectricMasterDataService;

    @Autowired
    private PmsRepairDataService pmsRepairDataService;

    @GetMapping("judge")
    @ApiOperation("判断是否有权限")
    public CommonResult<Object> judge(@RequestParam Integer type,
                                      @RequestParam Integer innerKind,
                                      @RequestParam String keyword) {
        UmsMember member = MemberContext.getMember();
        if (member == null) {
            return CommonResult.failed(ResultCode.UNAUTHORIZED);
        }
        if (type == 1) {
            List<PmsFactoryData> data = pmsFactoryDataService.list(new QueryWrapper<>(new PmsFactoryData())
                    .eq("name", keyword)
                    .eq("type", innerKind));
            if (data.isEmpty()) {
                return CommonResult.success(Collections.emptyList());
            }
            if (innerKind == 0){
                if (data.get(0).getShowStatus() == 1 && (member.getVip() == 0 || member.getVipExpireTime().getTime() < System.currentTimeMillis())) {
                    return CommonResult.failed(ResultCode.NEED_VIP);
                }
                return CommonResult.success(data.get(0));
            }else {
                for (PmsFactoryData pmsFactoryData : data) {
                    String[] split = pmsFactoryData.getUrl().split(pmsFactoryData.getName()+"/");
                    String title = split[1].replace(".html", "");
                    pmsFactoryData.setTitle(title);;

                    if (pmsFactoryData.getShowStatus() == 1 && (member.getVip() == 0 || member.getVipExpireTime().getTime() < System.currentTimeMillis())) {
                        return CommonResult.failed(ResultCode.NEED_VIP);
                    }
                }
                data.sort(Comparator.comparing(PmsFactoryData::getTitle));
                return CommonResult.success(data);
            }
        }
        if (type == 2){
            List<PmsCoolTool> list = pmsCoolToolService.list(new QueryWrapper<>(new PmsCoolTool())
                    .eq("name", keyword));
            for (PmsCoolTool coolTool : list) {
                if (coolTool.getShowStatus() == 1 && (member.getVip() == 0 || member.getVipExpireTime().getTime() < System.currentTimeMillis())) {
                    return CommonResult.failed(ResultCode.NEED_VIP);
                }
            }
            return CommonResult.success(null);
        }
        if (type == 3) {
            List<PmsElectricMasterData> list = pmsElectricMasterDataService.list(new QueryWrapper<>(new PmsElectricMasterData()).eq("name", keyword)
                    .eq("type", innerKind));
            for (PmsElectricMasterData pmsElectricMasterData : list) {
                if (pmsElectricMasterData.getShowStatus() == 1 && (member.getVip() == 0 || member.getVipExpireTime().getTime() < System.currentTimeMillis())) {
                    return CommonResult.failed(ResultCode.NEED_VIP);
                }
            }
            return CommonResult.success(null);
        }
        if (type == 4) {
            List<PmsRepairData> list = pmsRepairDataService.list(new QueryWrapper<>(new PmsRepairData()).eq("id", Long.parseLong(keyword)));
            for (PmsRepairData pmsRepairData : list) {
                if (pmsRepairData.getShowStatus() == 1 && (member.getVip() == 0 || member.getVipExpireTime().getTime() < System.currentTimeMillis())) {
                    return CommonResult.failed(ResultCode.NEED_VIP);
                }
            }
            return CommonResult.success(null);
        }
        return CommonResult.success("ok");
    }
}
